﻿#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Xml;
    using System.Xml.Linq;

    using Private = MarketPulse.BusinessLogic.Private;

#endregion

namespace MarketPulse.BusinessLogic.Legacy
{   
    #region Accreditation

        /// <summary>
        /// Class used to represent an Accreditation (shopper training course)
        /// </summary>
        public class Accreditation
        {
            #region Enumerations

                /// <summary>Enumeration used to determine the type of the page</summary>
                public enum PageType
                {
                    Start,
                    Information,
                    Title,
                    QuestionAnswerHorizontalFullPage,
                    QuestionAnswerHorizontal,
                    QuestionAnswerHorizontal3Row,
                    QuestionAnswerVertical,
                    QuestionAnswerTest,
                    Final,
                    Pass,
                    Fail
                }

                /// <summary>Enumeration used to determine the type of content for the page</summary>
                public enum PageContentType
                {
                    None,
                    PlainText,
                    TitleAndText,
                    QuestionAnswerHorizontal3Row,
                    QuestionAnswerHorizontal,
                    QuestionAnswerVertical,
                    QuestionAnswerTest,
                    Final,
                    Pass,
                    Fail
                }

            #endregion

            #region Member Variables

                /// <summary>Local accreditation object</summary>
                private Private.Accreditation mAccreditation;

                /// <summary>The xml parser to use to translate the xml into an accreditation object</summary>
                private AccreditationXmlParser mXmlParser; 

                /// <summary>The collection of pages for this ShopperAccreditation</summary>
                private AccreditationPageCollection mPages;

                /// <summary>The title for this ShopperAccreditation</summary>
                private string mTitle;
                      
            #endregion

            #region Constructors

                /// <summary>
                /// Default constructor for this class
                /// </summary>
                public Accreditation()
                {                        
                    mPages = new AccreditationPageCollection();
                    mXmlParser = new AccreditationXmlParser();
                }

            #endregion

            #region Methods

                // TODO add LoadAccreditations method to enable the collection to load in several accreditations based on a type (i.e. enabled, all etc...)
                public void LoadAccreditation(Guid accreditationGUID)
                {
                    try
                    {
                        mAccreditation = Private.Accreditation.Get(accreditationGUID);

                        // Give the Xml Parser the xml for the accreditation
                        mXmlParser.Xml = GetAccreditationXML();

                        // Set some basic values based on the XML parser
                        AccreditationGUID = mAccreditation.GUID.Value;
                        VersionID = mAccreditation.VersionID;
                        CompanyGUID = mAccreditation.CompanyGUID;
                        Title = mXmlParser.Title;
                        Description = mAccreditation.Description;
                        PassPercentage = mAccreditation.PassPercentage;
                        ShowSummary = mAccreditation.ShowSummary;
                        MaxAttempt = mAccreditation.MaxAttempts;
                        Pages = GetAccreditationPages();
                        Questions = GetAccreditationQuestions();
                    }
                    catch (Exception)
                    {
                        //TODO do something more meaningful here
                        throw;
                    }
                }

                /// <summary>
                /// Method used to update the status of the user in question with regards to this Accreditation
                /// </summary>
                /// <param name="accredited">Whether or not the user should be accredited</param>
                public void UpdateAccreditationAttempts(bool accredited)
                {
                    switch (accredited)
                    {
                        case true:
                            mAccreditation.AccreditUserAttempt(UserGUID);
                            break;
                        default:
                            mAccreditation.FailUserAttempt(UserGUID);
                            break;
                    }
                    mAccreditation = mAccreditation.Save();

                }

                #region Local Methods

                    /// <summary>
                    /// Method used to retrieve the XML for an Accreditation based on the accreditations xml content
                    /// </summary>                                                                    
                    /// <returns>A valid (filled) xml document object</returns>
                    private XmlDocument GetAccreditationXML()
                    {
                        // Instantiate the return object
                        XmlDocument temporaryXmlDocument = new XmlDocument();

                        try
                        {
                            // Use an xml reader to read the content of the XElement into and Xml doc before returning                                    
                            using (XmlReader xmlReader = (mAccreditation.AccreditationXML).CreateReader())
                            {
                                // load using the reader from the XElement                                   
                                temporaryXmlDocument.Load(xmlReader);
                            }
                        }
                        catch (Exception)
                        {
                            //TODO do something more meaningful here
                            throw;
                        }
                        return temporaryXmlDocument;
                    }

                    /// <summary>
                    /// Method used to retrieve a collection of pages for a given accreditation
                    /// </summary>
                    /// <returns>A valid (filled) Accreditation Page Collection</returns>
                    private AccreditationPageCollection GetAccreditationPages()
                    {
                        // Instantiate the return object
                        AccreditationPageCollection temporaryPages = new AccreditationPageCollection();

                        try
                        {
                            temporaryPages.SuccessPageNumber = mXmlParser.SuccessPageNumber;
                            temporaryPages.FailPageNumber = mXmlParser.FailPageNumber;

                            // Loop through the pages stroed in the xml and create page objects
                            for (int counter = 0; counter < mXmlParser.PageCount; counter++)
                            {
                                // Load the xml for a page
                                mXmlParser.ProcessPage(counter);

                                // Process the xml into a page object
                                AccreditationPage temporaryPage = new AccreditationPage();

                                // Set the accreditation properties
                                temporaryPage.BannerImage = mXmlParser.BannerImage;
                                temporaryPage.BannerTitle = mXmlParser.BannerTitle;
                                temporaryPage.ContentText = mXmlParser.ContentText;
                                temporaryPage.ContentTitle = mXmlParser.ContentTitle;
                                temporaryPage.ContentType = mXmlParser.PageContentType;
                                temporaryPage.ImageALT = mXmlParser.ImageALT;
                                temporaryPage.ImageSRC = mXmlParser.ImageSRC;
                                temporaryPage.InstructionText = mXmlParser.InstructionText;
                                temporaryPage.QuestionText = mXmlParser.QuestionText;
                                temporaryPage.SidebarImage = mXmlParser.SidebarImage;
                                temporaryPage.SidebarTitle = mXmlParser.SidebarTitle;
                                temporaryPage.Type = mXmlParser.PageType;
                                temporaryPage.Title = mXmlParser.PageTitle;
                                temporaryPage.ClientStylesheet = "Accreditations/" + mXmlParser.Title + "/Stylesheets/" + mXmlParser.Title + ".css";
                                temporaryPage.HeaderImage = "Accreditations/" + mXmlParser.Title + "/Images/logo.jpg";
                                temporaryPage.SeparatorImage = "Accreditations/" + mXmlParser.Title + "/Images/background-dot.jpg";
                                temporaryPage.SeparatorTitle = string.Empty;
                                Accreditation.PageType type = (Accreditation.PageType)Enum.Parse(typeof(Accreditation.PageType), temporaryPage.Type);

                                switch (type)
                                {
                                    case Accreditation.PageType.Title:
                                        temporaryPage.BannerTitleStyleClass = "titlePageBannerTitle";
                                        temporaryPage.ContentStyleClass = "startPageContent";
                                        temporaryPage.SeparatorStyleClass = "startPageSeparator";
                                        break;
                                    case Accreditation.PageType.Start:
                                        temporaryPage.ContentStyleClass = "startPageContent";
                                        temporaryPage.BannerTitleStyleClass = "startPageBannerTitle";
                                        temporaryPage.SeparatorStyleClass = "startPageSeparator";
                                        break;
                                    case Accreditation.PageType.QuestionAnswerVertical:
                                    case Accreditation.PageType.QuestionAnswerHorizontal:
                                    case Accreditation.PageType.QuestionAnswerHorizontal3Row:
                                        temporaryPage.ContentStyleClass = "normalPageContent";
                                        temporaryPage.SeparatorStyleClass = "normalPageSeparator";
                                        temporaryPage.SeparatorTitle = mXmlParser.ContentTitle;
                                        // Get the display question for this AccreditationPage
                                        temporaryPage.DisplayQuestion = mXmlParser.GetDisplayQuestion();
                                        break;
                                    case Accreditation.PageType.QuestionAnswerHorizontalFullPage:
                                        temporaryPage.HeaderImage = "";
                                        temporaryPage.ContentStyleClass = "questionAnswerPageContent";
                                        temporaryPage.SeparatorStyleClass = "noSeparator";
                                        temporaryPage.DisplayQuestion = mXmlParser.GetDisplayQuestion();
                                        break;
                                    case Accreditation.PageType.QuestionAnswerTest:
                                        temporaryPage.ContentStyleClass = "normalPageContent";
                                        temporaryPage.SeparatorStyleClass = "normalPageSeparator";

                                        // Get the questions for this AccreditationPage
                                        //temporaryPage.Questions = mXmlParser.GetQuestions();
                                        break;
                                    case Accreditation.PageType.Information:
                                        temporaryPage.ContentStyleClass = "normalPageContent";
                                        temporaryPage.SeparatorStyleClass = "normalPageSeparator";
                                        break;
                                    case Accreditation.PageType.Final:
                                    case Accreditation.PageType.Pass:
                                    case Accreditation.PageType.Fail:
                                        temporaryPage.ContentStyleClass = "finishPageContent";
                                        temporaryPage.BannerTitleStyleClass = "finishPageBannerTitle";
                                        temporaryPage.SeparatorStyleClass = "finishPageSeparator";
                                        break;
                                }
                                // Add the page to the accreditation page collection
                                temporaryPages.Add(temporaryPage);
                            }
                        }
                        catch (Exception)
                        {
                            //TODO do something more meaningful here
                            throw;
                        }
                        return temporaryPages;
                    }

                    /// <summary>
                    /// Method used to retrieve a collection of questions for a given accreditation
                    /// </summary>
                    /// <returns>A valid (filled) Question Collection</returns>
                    private QuestionCollection GetAccreditationQuestions()
                    {
                        // Instantiate the return object
                        QuestionCollection temporaryQuestions = new QuestionCollection();

                        try
                        {
                            temporaryQuestions = mXmlParser.GetAllQuestions();
                        }
                        catch (Exception)
                        {
                            //TODO do something more meaningful here
                            throw;
                        }
                        return temporaryQuestions;
                    }

                #endregion

            #endregion

            #region Properties

                //public Private.Accreditation InternalAccreditation { set { mAccreditation = value; } }

                /// <summary>The Accreditation ID for this accreditation</summary>
                public Guid AccreditationGUID { get; set; }

                /// <summary>The Version ID for this accreditation</summary>
                public long VersionID { get; set; }

                /// <summary>The guid for user associated with this accreditation</summary>
                public Guid? UserGUID { get; set; }

                /// <summary>Whether the user associated with this accreditation is already accredited or not.</summary>
                public bool UserAccredited { get; set; }

                /// <summary>The guid for Company associated with this accreditation</summary>
                public Guid? CompanyGUID { get; set; }

                /// <summary>The title of this ShopperAccreditation</summary>
                public string Title { get { return mTitle; } set { mTitle = value; } }

                /// <summary>The show summary flag to control whether adding the summary link in the fail page</summary>
                public bool ShowSummary { get; set; }

                /// <summary>The Max Attempt number of the accreditation</summary>
                public int MaxAttempt { get; set; }

                /// <summary>The pages for this ShopperAccreditation</summary>
                public AccreditationPageCollection Pages { get { return mPages; } set { mPages = value; } }

                /// <summary>The result of the accreditation</summary>
                public bool Result { get; set; }

                /// <summary>A friendly description of this Accreditation.</summary>
                public string Description { get; set; }

                /// <summary>The number of the current page of the Accreditation</summary>
                public int CurrentPageNumber { get; set; }
                            
                /// <summary>The minimum required percentage a user must score to be accredited.</summary>
                public decimal PassPercentage { get; set; }

                /// <summary>The number of pages in this accreditation</summary>
                public int PageCount { get { return mPages.Count; } }

                /// <summary>The page number of this accreditation 'Pass' page.</summary>
                public int PassPageNumber { get { return mPages.SuccessPageNumber; } }

                /// <summary>The page number of this accreditation 'Fail' page.</summary>
                public int FailPageNumber { get { return mPages.FailPageNumber; } }

                /// <summary>The current page for this accreditation</summary>
                public AccreditationPage CurrentPage { get { return mPages[CurrentPageNumber]; } }

                /// <summary>The Collection of Questions for this page</summary>
                public QuestionCollection Questions { get; set; }

                /// <summary>The number of attempts at being accredited based on the associated user</summary>
                public int Attempts { get { return mAccreditation.GetAttemptsByUser(this.UserGUID, false); } }

            #endregion
        }

    #endregion
}
